package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.system.domain.TDeviceFault;
import com.ruoyi.system.domain.TDeviceMaintain;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.IMainService;
import com.ruoyi.system.service.ITDeviceFaultService;
import com.ruoyi.system.service.ITDeviceMaintainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api(tags = "(客户端) 主界面")
@RestController
@RequestMapping("/client/main")
public class MainController {
    @Autowired
    private IMainService mainService;
    @Autowired
    private ITDeviceFaultService deviceFaultService;
    @Autowired
    private ITDeviceMaintainService maintainService;

    @GetMapping("/finishSchedule")
    @ApiOperation(value = "总体进度完成情况")
    public ResultEntity<ProdPlanVO> finishSchedule(@RequestParam(value = "projectId") Long projectId){
        return ResultEntity.createSuccess(mainService.finishSchedule(projectId));
    }

    @GetMapping("/taskFinishSchedule")
    @ApiOperation(value = "生产任务完成情况")
    public ResultEntity<ProdPlanVO.PlanDetail> taskFinishSchedule(@RequestParam(value = "projectId") Long projectId){
        return ResultEntity.createSuccess(mainService.taskFinishSchedule(projectId));
    }

    @GetMapping("/prodFinishSchedule")
    @ApiOperation(value = "各产品生产占比")
    public ResultEntity<List<PieVO>> prodFinishSchedule(@RequestParam(value = "projectId") Long projectId){
        return ResultEntity.createSuccess(mainService.prodFinishSchedule(projectId));
    }

    @GetMapping("/prodYearTotal")
    @ApiOperation(value = "每年各月份生产情况")
    public ResultEntity<List<ProdYearTotalVO>> prodYearTotal(@RequestParam(value = "projectId") Long projectId){
        return ResultEntity.createSuccess(mainService.prodYearTotal(projectId));
    }

    @GetMapping("/prodMonthTotal")
    @ApiOperation(value = "查询月份每天产量情况")
    public ResultEntity<ProdMonthTotalVO> prodMonthTotal(@RequestParam(value = "projectId") Long projectId,
                                                         @RequestParam(value = "monthDate") String monthDate){
        return ResultEntity.createSuccess(mainService.prodMonthTotal(projectId,monthDate));
    }

    @GetMapping("/warning")
    @ApiOperation(value = "设备故障/维护提示",response = WarningVO.class)
    public ResultEntity warning(@RequestParam(value = "projectId") Long projectId){
        List<WarningVO> list = new ArrayList<>();
        List<TDeviceFault> faultList = deviceFaultService.list(new LambdaQueryWrapper<TDeviceFault>()
                .eq(TDeviceFault::getStatus,0));
        faultList.forEach(item -> {
            WarningVO vo = new WarningVO();
            vo.setType(1);
            vo.setContent(item.getName());
            list.add(vo);
        });
        List<TDeviceMaintain> maintainList = maintainService.list(new LambdaQueryWrapper<TDeviceMaintain>()
                .eq(TDeviceMaintain::getStatus,0));
        maintainList.forEach(item -> {
            WarningVO vo = new WarningVO();
            vo.setType(2);
            vo.setContent(item.getContent());
            list.add(vo);
        });
        return ResultEntity.createSuccess(list);
    }

}
